//
//  XZ_infoView.m
//  Project_Demo
//
//  Created by caoxiaozhu on 2018/7/21.
//  Copyright © 2018年 caoxiaozhu. All rights reserved.
//

#import "XZ_infoView.h"

static NSString *const cellID = @"cellID";

@interface XZ_infoView()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UIScrollView *mainScrollView;
@property(nonatomic,strong)UITableView *optionTableView;
@property(nonatomic,assign)CGFloat bottomBtnStartY;
@property(nonatomic,strong)NSString *bottomBtnStr;
@property(nonatomic,assign)NSInteger bottomBtnTag;
@end

@implementation XZ_infoView
SINGLETON_FOR_CLASS(XZ_infoView);

-(void)layoutSubviews{
    [super layoutSubviews];
    [self setupUI];
}


-(void)setupUI{
    //mainScrollView
    _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _mainScrollView.backgroundColor = Color_Clear;
    _mainScrollView.showsVerticalScrollIndicator = false;
    [self addSubview:_mainScrollView];
    //卡片
    UIImageView *cardBacImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"卡片详情4"]];
    cardBacImageView.frame = CGRectMake(15, 10, kScreenWidth - 30, percentageScreenHeight *2.5);
    [_mainScrollView addSubview:cardBacImageView];
    
    CGFloat startY = 15;
    if (IS_iPhone6_Plus) {
        startY = 17;
    }
    
    UILabel *cardTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(percentageScreenWidth *1.6, startY, percentageScreenWidth * 5, 20)];
    cardTitleLabel.text = @"合肥通电子公交卡";
    [cardBacImageView addSubview:cardTitleLabel];
    
    UILabel *cardNoLabel = [[UILabel alloc]initWithFrame:CGRectMake(cardTitleLabel.left, cardTitleLabel.bottom, cardTitleLabel.width, cardTitleLabel.height)];
    cardNoLabel.text = [NSString stringWithFormat:@"NO.%@",_cardStr];
    if (_cardStr && _isAlreadyGetCard) {
        [cardBacImageView addSubview:cardNoLabel];
    }else{
        cardTitleLabel.bottom = 45;
    }
    
    
    //底部的余额
    
    UILabel *amountTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(percentageScreenWidth * 0.5, percentageScreenHeight * 1.5, percentageScreenWidth *2, 30)];
    amountTitleLabel.text = @"余额:";
    amountTitleLabel.font = cardTitleLabel.font = cardNoLabel.font = FONT(15);
    
    UILabel *amountLabel = [[UILabel alloc]initWithFrame:CGRectMake(amountTitleLabel.left, amountTitleLabel.bottom, percentageScreenWidth *3, 40)];
    amountLabel.text = [NSString stringWithFormat:@"%@元",_amount];
    amountLabel.font = FONT(30);
    amountLabel.textColor = amountTitleLabel.textColor = cardTitleLabel.textColor = cardNoLabel.textColor = Color_White;
    
    if (self.amount && _isAlreadyGetCard) {
        [cardBacImageView addSubview:amountTitleLabel];
        [cardBacImageView addSubview:amountLabel];
    }
    
    //下面4按钮table
    //如果已经领卡，则显示正常按钮，如果没有领卡则显示领卡按钮
    if (_isAlreadyGetCard) {
        _optionTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, cardBacImageView.bottom+15, kScreenWidth, 241) style:UITableViewStyleGrouped];
        _optionTableView.dataSource = self;
        _optionTableView.delegate = self;
        _optionTableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
        _optionTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
        _optionTableView.scrollEnabled = false;
        _optionTableView.rowHeight = 60;
        [_mainScrollView addSubview:_optionTableView];
        
        _bottomBtnStartY = _optionTableView.bottom+15;
        _bottomBtnStr = @"立即使用";
        _bottomBtnTag = 0;
    }else{
        UILabel *cardFirTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, cardBacImageView.bottom+25, kScreenWidth, 30)];
        cardFirTitleLabel.centerX = cardBacImageView.centerX;
        cardFirTitleLabel.text = @"合肥市城市卡";
        cardFirTitleLabel.font = FONT(25);
        [_mainScrollView addSubview:cardFirTitleLabel];
        
        UILabel *cardSecTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, cardFirTitleLabel.bottom+25, kScreenWidth, 30)];
        cardSecTitleLabel.centerX = self.centerX;
        cardSecTitleLabel.text = @"-扫码乘车 快捷支付-";
        cardSecTitleLabel.font = FONT(18);
        [_mainScrollView addSubview:cardSecTitleLabel];
        
        UILabel *cardThirTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, cardSecTitleLabel.bottom+5, kScreenWidth, 30)];
        cardThirTitleLabel.centerX = self.centerX;
        cardThirTitleLabel.text = @"告别零钱与找卡，随扫随走更便捷";
        cardThirTitleLabel.font = FONT(15);
        cardThirTitleLabel.textColor = Color_Gray_Light;
        cardFirTitleLabel.textAlignment = cardSecTitleLabel.textAlignment = cardThirTitleLabel.textAlignment = NSTextAlignmentCenter;
        [_mainScrollView addSubview:cardThirTitleLabel];
        
        _bottomBtnStartY = cardThirTitleLabel.bottom + 15;
        _bottomBtnStr = @"立即领卡";
        _bottomBtnTag = 1;
    }
    
    //底部的按钮
    UIButton *useBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    useBtn.frame =CGRectMake(15, _bottomBtnStartY, kScreenWidth-30, 50);
    [useBtn setTitle:@"立即使用" forState:UIControlStateNormal];
    [useBtn setBackgroundImage:[UIImage imageWithColor:[UIColor hex:@"0f6ab5"]] forState:UIControlStateNormal];
    useBtn.tag = _bottomBtnTag;
    [useBtn addTarget:self action:@selector(clickToUse:) forControlEvents:UIControlEventTouchUpInside];
    [_mainScrollView addSubview:useBtn];
    _mainScrollView.contentSize = CGSizeMake(0, useBtn.bottom + naviHeight);
    
}

#pragma mark - 立即使用
-(void)clickToUse:(UIButton *)btn{
//    [self.mainScrollView ]
    [self.delegate immediatelyUseBtnAction:_cardStr BtnTag:btn.tag];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.chooseTitleArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell ==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.imageView.image = [UIImage imageNamed:_chooseImageArr[indexPath.row]];
    cell.textLabel.text = _chooseTitleArr[indexPath.row];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    [self.delegate chooseWhichTag:indexPath.row];
}



@end
